<template>
  <el-dialog
    :title="title"
    :visible.sync="addVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @click="handleCancel"
  >
    <el-form :model="bannerInfo">
      <el-form-item
        label="新增Banner"
        :label-width="formLabelWidth"
        v-if="!isAdd"
      >
      </el-form-item>
      <el-form-item label="banner编号" :label-width="formLabelWidth">
        <el-input v-model="bannerInfo.id"></el-input>
      </el-form-item>
      <el-form-item label="广告名称" :label-width="formLabelWidth">
        <el-input v-model="bannerInfo.title"></el-input>
      </el-form-item>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status ? 'success' : 'danger'">
            {{ scope.row.status ? "启用" : "关闭" }}
          </el-tag>
        </template>
      </el-table-column>

      <!-- <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          style="display: block"
          v-model="bannerInfo.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="关闭"
          inactive-text="开启"
        >
        </el-switch>
      </el-form-item> -->

      <!-- 上传头像 -->
      <el-form-item label="头像" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="/api/auth/banner/upload"
          :show-file-list="false"
          name="icon"
          :headers="headers"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="bannerInfo.icon" :src="bannerInfo.icon" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </div>
  </el-dialog>
</template>
  
<script>
import {
  bannerInfoByIdApi,
  addBannerApi,
  editBannerApi,
} from "@/Api/BannerApi"; // 添加API引用
export default {
  props: {
    addVisible: Boolean,
    title: String,
  },
  data() {
    return {
      bannerInfo: {
        id: "",
        title: "",
        icon: "",
        status: 1,
        isDelete: 0,
      },
      headers: {
        Authorization: "Bearer " + localStorage.getItem("token"),
      },
      isAdd: true,
      formLabelWidth: "120px",
    };
  },
  methods: {
    handleCancel() {
      this.$emit("closeAddDialog");
    },
    async handleSave() {
      try {
        const apiMethod = this.isAdd ? addBannerApi : editBannerApi;
        const res = await apiMethod({
          ...this.bannerInfo,
          status: this.bannerInfo.status ? 1 : 0, // 确保状态值正确
        });

        if (res.status === 200) {
          this.$emit("closeAddDialog", true);
          this.$message.success(res.msg);
        }
      } catch (error) {
        this.$message.error("操作失败");
      }
    },
    // async handleSave() {
    //   try {
    //     let res;
    //     if (this.isAdd) {
    //       res = await addBannerApi(this.bannerInfo);
    //     } else {
    //       res = await editBannerApi(this.bannerInfo);
    //     }

    //     if (res.status === 200) {
    //       this.$message.success(res.msg);
    //       this.$emit("closeAddDialog", true); // 传递刷新标志
    //     } else {
    //       this.$message.error(res.msg);
    //     }
    //   } catch (error) {
    //     this.$message.error("系统异常");
    //   }
    // },
    async openDialog(bannerId) {
      this.isAdd = !bannerId; // 关键修复逻辑
      if (this.isAdd) {
        this.resetForm();
      } else {
        try {
          const res = await bannerInfoByIdApi(bannerId);
          res.status === 200
            ? (this.bannerInfo = res.data)
            : this.$message.error(res.msg);
        } catch (error) {
          this.$message.error("系统异常");
        }
      }
    },
    // async openDialog(bannerId) {
    //   if (!bannerId) {
    //     this.resetForm();
    //     this.isAdd = true;
    //   } else {
    //     this.isAdd = false;
    //     try {
    //       const res = await bannerInfoByIdApi(bannerId);
    //       if (res.status == 200) {
    //         this.bannerInfo = res.data;
    //         console.log(res.data);
    //       } else {
    //         this.$message({
    //           message: res.msg,
    //           type: "error",
    //           duration: 1000,
    //         });
    //       }
    //     } catch (error) {
    //       this.$message({
    //         message: "系统异常",
    //         type: "error",
    //         duration: 1000,
    //       });
    //     }
    //   }
    // },
    //上传校验之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess(res) {
      if (res.status === 200) {
        this.bannerInfo.icon = `${res.data.hostName}${res.data.fileName}`;
      } else {
        this.$message.error(res.msg);
      }
    },
    //上传成功
    // handleAvatarSuccess(res, file) {
    //   if (res.status == 200) {
    //     const fileName = res.data.fileName;
    //     const hostName = res.data.hostName;
    //     this.bannerInfo.icon = hostName + fileName; // 修正字段为icon
    //   } else {
    //     this.$message.error(res.msg);
    //   }
    // },
    resetForm() {
      this.bannerInfo = {
        id: "",
        title: "",
        icon: "",
        status: 1,
        isDelete: 0,
      };
    },
  },
};
</script>
  
  <style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>